﻿using System;
using System.Data;
using Business;
using Common;
using Common.Constant;
using Entity;

namespace admincp
{
    public partial class Categories : System.Web.UI.Page
    {
        private CategoryBus _categoryBus;
        private Category _category;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["User"] == null || Session["Role"].ToString() != DBSchemas.AdminRoleName)
            {
                Response.Redirect("~/Default.aspx");
            }
            if (Request.QueryString["id"] != null && Request.QueryString["id"] != "" && Request.QueryString["ac"] == "edit")
            {
                try
                {
                    LoadEditData(int.Parse(Request.QueryString["id"]));
                    pnEditor.Visible = true;
                    pnView.Visible = false;
                }
                catch (Exception exception)
                {
                    Response.Write(exception.Message);
                }
            }
            else if (Request.QueryString["id"] != null && Request.QueryString["id"] != "" && Request.QueryString["ac"] == "del")
            {
                try
                {
                    _categoryBus = new CategoryBus();
                    _categoryBus.DeleteCategory(int.Parse(Request.QueryString["id"]));
                    Response.Redirect("~/admincp/Categories.aspx");
                }
                catch (Exception exception)
                {
                    //Response.Write(exception.Message);
                    UIMessage.ShowMessage("Không thể xóa danh mục này",this);
                }
            }
            else
            {
                try
                {
                    LoadListCategory();
                    pnView.Visible = true;
                    pnEditor.Visible = false;
                }
                catch (Exception exception)
                {
                    Response.Write(exception.Message);
                }
            }

        }
        protected void LoadListCategory()
        {
            _categoryBus = new CategoryBus();
            DataSet list = _categoryBus.SelectAllCategory();
            RepeaterListCategory.DataSource = list;
            RepeaterListCategory.DataBind();
        }

        protected void ButtonSubmitClick(object sender,EventArgs e)
        {
            try
            {
                _categoryBus = new CategoryBus();
                if (ButtonSubmit.Text == "Lưu sửa đổi")
                {
                    _category = new Entity.Category()
                    {
                        CatId = int.Parse(Request.QueryString["id"]),
                        CatName = TextBoxName.Text,
                        CatDescription = TextBoxDescription.Text
                    };
                    UIMessage.ShowMessage(
                        _categoryBus.UpdateCategory(_category) ? "Chỉnh sửa thành công" : "Chỉnh sửa thất bại", this);
                }
                else
                {
                    _category = new Entity.Category()
                                    {
                                        CatName = TextBoxName.Text,
                                        CatDescription = TextBoxDescription.Text,
                                        PostCount = 0
                    };
                    UIMessage.ShowMessage(_categoryBus.AddCategory(_category) ? "Thêm danh mục thành công !" : "Thêm danh mục thất bại !", this);
                }

            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }

        protected void LoadEditData(int id)
        {
            _categoryBus = new CategoryBus();
            DataSet category = _categoryBus.GetCategoryById(id);
            if (category.Tables[0].Rows.Count > 0)
            {
                TextBoxName.Text = category.Tables[0].Rows[0]["CatName"].ToString();
                TextBoxName.Text = category.Tables[0].Rows[0]["CatDescription"].ToString();
                ButtonSubmit.Text = "Lưu sửa đổi";
            }
        }
    }
}
